﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace HzyAdmin
{
    using Aop;
    using DbFrame.DbContext.SqlServer;
    using Toolkit;
    using Toolkit.LogService;

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //注入链接字符串
            DbFrame.DbContext.SqlServer.DbContextSqlServer.Register(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(), (tabs) =>
            {
                ////系统表 Start
                //tabs.Register(typeof(Entitys.SysClass.Sys_Function));
                //tabs.Register(typeof(Entitys.SysClass.Sys_Menu));
                //tabs.Register(typeof(Entitys.SysClass.Sys_MenuFunction));
                //tabs.Register(typeof(Entitys.SysClass.Sys_Role));
                //tabs.Register(typeof(Entitys.SysClass.Sys_RoleMenuFunction));
                //tabs.Register(typeof(Entitys.SysClass.Sys_User));
                //tabs.Register(typeof(Entitys.SysClass.Sys_UserRole));
                ////系统表 End
                //tabs.Register(typeof(Entitys.Member));
                Entitys.Class.EntitySet.Register(tabs);
            });

            LogHelper.CreateRepository(Server.MapPath("/Log4Net/log4net.config"));

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //BundleConfig.RegisterBundles(BundleTable.Bundles);

            //注册自定义视图
            ViewEngines.Engines.Clear();                    // 清除原MVC视图引擎规则
            ViewEngines.Engines.Add(new CustomViewEngine());  // 使用自定义视图引擎

            Tools.Log.WriteLog("应用程序已启动");
        }

        protected void Application_Error()
        {
            if (Server.GetLastError() != null) Tools.Log.WriteLog((Exception)Server.GetLastError().GetBaseException(), Request.UserHostAddress);
        }

        protected void Application_End()
        {
            //  在应用程序关闭时运行的代码
            Tools.Log.WriteLog("应用程序已结束");
        }


    }
}
